package com.wy.powerbuilder.controller.hxl;
import com.github.pagehelper.PageInfo;
import com.wy.powerbuilder.entity.hxl.PageEntity;
import com.wy.powerbuilder.entity.hxl.carseat;
import com.wy.powerbuilder.mapper.hxl.carseatMapperAll;
import com.wy.powerbuilder.service.hxl.carseatAllService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class carseatAllController {
    @Autowired
    private carseatMapperAll carsetall;
    @Autowired
    private carseatAllService serviceall;



    //车位管理id查
    @RequestMapping(value = {"/singleid"})
    public String seleid(int cs_id, Model model) {
        carseat smbid = new carseat();
        smbid  = serviceall.singleselectidall(cs_id);
        model.addAttribute("smbid", smbid);
        return "editor";
    }
    //车位管理修改
    @RequestMapping(value = {"/singleupdate"})
    public String editorapdate(carseat cars,Model model) {
        int smbid  = serviceall.singleupdate(cars);
        return "redirect:/managementAll";
    }


   /* public String getPage(HttpServletRequest request, Integer pageNo, Integer pageSize,String cs_state, Model model)throws Exception{
        PageEntity page=new PageEntity();
        if (pageSize!=null) {
            page.setPageSize(pageSize);
        }
        if (pageNo!=null) {
            page.setPageNo(pageNo);
        }
        if (cs_state!=null) {
            page.setCs_state(cs_state);
        }
        int sum=serviceall.singlesgetCountall(page);
        int count=sum%page.getPageSize()==0?sum/page.getPageSize()
                :sum/page.getPageSize()+1;
        page.setTotalPages(count);
        List<carseat> list=serviceall.singlesgetPageall(page);
        model.addAttribute("thePage",page);
        model.addAttribute("pageInfo",list);
        HttpSession session=request.getSession();
        session.setAttribute("sum",sum);
        //model.addAttribute("sum",sum);


    }*/

    //车位管理分页
    @RequestMapping(value = {"/managementAll"})
    public String selByPage(Integer pageNum, Model model,String cs_state){
        //实现我们的分页  每页显示两条   pageInfo应该放在那里 model modelAndView map
        PageInfo<carseat> pageInfo = serviceall.selByPage(pageNum, 10,cs_state);
        model.addAttribute("pageInfo",pageInfo);
        if(cs_state==null){
            cs_state="";
        }
        model.addAttribute("cs_state",cs_state);
        return "managementAll";
    }

    //车位管理删除
    @RequestMapping(value = {"/singledelete"})
    public String delete(Integer cs_id) {
        int result = serviceall.singledelete(cs_id);
        if (result>0){
            return "redirect:/managementAll";
        }
        return "error";
    }
    //车位管理批量删除
    @ResponseBody
    @RequestMapping("/singledeleteall")
    public String deleteall(String cs_id) {
/*
       for (int i=0;i<cs_id.length();i++){
        String subStr = cs_id.substring(i,cs_id.indexOf(","));
        String str2= cs_id.substring(subStr.length()+1,cs_id.length());

              System.out.println(str2 +"hh");
              abc = car.deleteall(str2);
     }*/
        int i=0;
        int len=3;
        for (;i+len<=cs_id.length();i+=len){
            System.out.println(cs_id.substring(i,i+len)+"333");
            System.out.println(i+"111");
            System.out.println(len+"222");
            int abc = serviceall.singledeleteall(cs_id.substring(i,i+len));
        }
        return "success";

    }
}
